/*
day479 
2020.12.19 
*/
#include<iostream>
#include<vector>
using namespace std;


class Node {
public:
    int val;
    Node* prev;
    Node* next;
    Node* child;
};


class Solution {
public:
    Node* flatten(Node* head) {
        help(head);
        return head;
    }
    
    vector<Node *> help(Node* head)
    {
    	Node *p=head,*r,*teml,*temr;
    	while(p) 
		{
			if(p->child)
			{
				vector<Node *> tem=help(p->child);
				tem[0]->prev=p;
				tem[1]->next=p->next;
				p->next=tem[0];
				tem[1]->next->prev=tem[1];
				p=tem[1];
				p->child=nullptr; 
			}
			r=p;
			p=p->next;
		}
    	return {head,r};
	}
    
};

int main()
{
	return 0;
}
